/*
 * File    : /src/api/mark/onlineCheckV1.js
 * Time    : 2021-10-26 17:33:42
 * Author  : Gray Snail
 * Version : 1.0
 * Contact : shtao2011@sina.com
 * License : Copyright (c) 2021 Gray Snail
 * Desc    : None
 */

import axios from 'axios'
import request from '@/utils/request'
import { getToken } from '@/utils/auth'
import localforage from 'localforage'

export function pointsCloudDetails(qaResultId) {
  const data = {
    qaResultId,
  }
  return request({
    url: '/mark/onlineCheckV1/pointsCloudDetails',
    method: 'get',
    params: data,
  })
}

export function getMarkAnnotations(taskId) {
  const data = {
    taskId,
  }
  return request({
    url: '/mark/onlineCheckV1/getMarkAnnotations',
    method: 'get',
    params: data,
  })
}

export function imageFile(qaResultId, imgIndex) {
  const data = {
    qaResultId,
    imgIndex,
  }
  return request({
    url: '/mark/onlineCheckV1/imageFile',
    method: 'get',
    params: data,
  })
}

export function pcdBlob(fileName) {
  let url = process.env.VUE_APP_BASE_API + '/mark/onlineCheckV1/pcdBlob?fileName=' + encodeURI(fileName)
  return axios({
    method: 'get',
    url: url,
    responseType: 'blob',
    headers: { Authorization: 'Bearer ' + getToken() },
  })
}

export function pcdBlobWithFlag(fileName, pcdFlag) {
  let url =
    process.env.VUE_APP_BASE_API +
    '/mark/onlineCheckV1/pcdBlobWithFlag?fileName=' +
    encodeURI(fileName) +
    '&pcdFlag=' +
    encodeURI(pcdFlag)
  return axios({
    method: 'get',
    url: url,
    responseType: 'blob',
    headers: { Authorization: 'Bearer ' + getToken() },
  })
}

// 获取图片文件,优先从缓存获取
export async function getImageWithCache(mQaResultId, imgIndex, annotationCache) {
  const imageKey = mQaResultId + ':img' + imgIndex
  const cacheKey = imageKey

  let imageCache = await localforage.getItem(cacheKey)
  // console.log(imageCache, annotationCache)

  if (annotationCache && imageCache) {
    return { ...imageCache, annotation: annotationCache }
  }

  const response = await imageFile(mQaResultId, imgIndex)
  const { imageSrc, annotation } = response

  let image
  if (imageSrc.startsWith('http')) {
    if (imageCache) {
      image = imageCache.blob
    } else {
      const imageResponse = await axios({ method: 'get', url: imageSrc, responseType: 'blob' })
      image = imageResponse.data
    }
  } else {
    image = 'data:image/jpg;base64,' + imageSrc
  }

  if (!imageCache) {
    imageCache = { blob: image, filename: annotation.fileName, mQaResultId }
    await localforage.setItem(cacheKey, imageCache)
  } else {
    console.log('Load frame/annotation from server', imageKey)
  }

  // console.log(image, annotation)
  return { blob: image, annotation }
}

export async function getPcdFileWithCache(resultId) {
  try {
    const data = await localforage.getItem(resultId + '')
    if (data == null) {
      throw new Error('no cache pcd file: ' + resultId)
    }
    return data
  } catch (error) {
    const response = await pcdBlob(resultId)
    if (!response) {
      return null
    }
    // console.log(response)
    if (response.data instanceof Blob && response.data.type === 'text/plain') {
      // load pcd file from url
      const url = await response.data.text()
      const data = await axios({ method: 'get', url, responseType: 'blob' })
      // console.log(data)
      const blob = new Blob([data.data], { type: 'application/octet-stream' })
      const _data = { blob: blob, file_name: resultId }
      await localforage.setItem(resultId + '', _data)
      return _data
    } else if (response.data instanceof Blob && response.data.type == 'application/octet-stream') {
      const _data = { blob: response.data, file_name: resultId }
      await localforage.setItem(resultId + '', _data)
      return _data
    } else {
      console.warn('error response type', response)
    }
  }
}
